<?php
namespace app\ucentre\controller;

use app\ucentre\WenBaseController;
use app\ucentre\model\User;
use app\ucentre\model\Active;
use app\ucentre\service\PwUser;
use wendasns\facade\Email;
use wendasns\Wend;
use think\helper\Str;

class Forget extends WenBaseController
{
    public function index()
    {
    	return view();
    }

    public function check()
    {
    	$username = input('post.username', '');

        $dm = User::getByUsername($username);
        if(!$dm){
        	error('用户名不存在');
        }
        list($name, $domain) = explode('@',$dm->email);
        $first = substr($name,0,1);
        $tail = substr($name,-1,1);
        $email = $first.'******'.$tail.'@'.$domain;
    	return view('index',['username'=>$dm->username,'email'=>$email]);
    }
    
    public function send()
    {
    	$username = input('post.username', '');
    	$email = input('post.email', '');

        $dm = User::where('username',$username)->find();

        if(!$dm || $email<>$dm->email){
        	error('用户名不存在或邮箱不正确');
        }
        $active = Active::where('user_id',$dm->id)->where('typeid',2)->find();
        if($active){
        	$active->code = Str::random(16);
        	$active->create_time = Wend::getTime();
        	$active->active_time = 0;
        	$active->save();
        }else{
	        $active = Active::create([
	        	'user_id'=>$dm->id,
	        	'email'=>$dm->email,
	        	'code'=>Str::random(16),
	        	'create_time'=>Wend::getTime(),
	        	'typeid'=>2
	        ]);
        }
        
        $url = (string)url('ucentre/forget/resetpwd',['code'=>$active->code])->domain(true);
        
        try{
        	\wendasns\facade\Email::send($dm->email, $username.'您好，这是wendasns发送给您的密码重置邮件', $url);
    	}catch (\think\Exception $e) {
    		error($e->getMessage());
    	}
    	return view('index');
    }
    
    public function resetpwd()
    {
    	$code = input('get.code', '');
    	$dm = Active::where('code',$code)->find();
    	if(!$dm){
    		error('链接已失效，请重新再试！');
    	}
    	$times = Wend::getTime();
    	if($dm->active_time){
    		error('已验证');
    	}
    	if(request()->isPost()){
	    	$password = input('post.password', '');
	    	$repassword = input('post.repassword', '');
	    	
	    	$dm = Active::where('code',$code)->find();
	    	
	        //验证数据，包括敏感词
	        try {
	            validate(\app\ucentre\validate\User::class)->check([
	                'password' => $password,
	                'repassword' => $repassword
	            ]);
	        } catch (\think\exception\ValidateException $e) {
	            error($e->getError());
	        }

	        try {
	            $user = new PwUser();
	            $user->id = $dm->user_id;
	            $user->password = $password;
	            $user->update();
	        } catch (\think\Exception $e) {
	            error($e->getMessage());
	        }
	        $dm->active_time = Wend::getTime();
	        $dm->save();
	        success('密码修改成功');
    	}
    	return view('index',['username'=>$dm->users->username]);
    }
}